转换字符串中的索引? [英] Shift the indexes in a string?

查看:123
本文介绍了转换字符串中的索引?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在字符串中移动索引,假设我已经声明了一个字符串,即



String str;

cin>> ; str;

//现在,我想修改相同的字符串,因为我想将

//字符串中的元素移动一个特定的编号。

//例如



str =你好;

//来自h的移位2宫殿。 />
str =h ello;

//我不想使用字符数组。



我尝试过:



//我们将不胜感激任何帮助。

how can i shift the indexes in a string,suppose i have declared a string i.e

String str;
cin>>str;
//now,i want to modify the same string as i want to shift the elements in the
// string by a particular no.
//for example

str="hello";
// say shift 2 palaces from h.
str="h ello ";
//i do not want to use character array.

What I have tried:

// Any help will be appreciated.

推荐答案

如果你需要在字符串中的某处插入空格(如你的例子所示),那么 string :: insert 方法(参见string::insert - C ++ Reference [ ^ ])是你的朋友:

If you need to insert blanks somewhere in the string (like your example suggests) then the string::insert method ( see string::insert - C++ Reference[^]) is your friend:
#include <iostream>
#include <string>

using namespace std;

int main()
{
  string s = "hello";
  s.insert(1, 1, ' ');
  s.insert(s.end(), 1, ' ');
  cout << "'" << s << "'" << endl;
}


你的问题不清楚。



你的例子他喜欢没有移动但插入了空间。换班操作会导致类似ello(左移一个)或你好(右移一个;也可能是地狱,取决于班次定义)。



检查String类提供的方法。可能有可以使用的函数(候选者是insert(),substr(),left(),right(),mid())。



另请注意一个字符串类通常只是一个动态字符数组。
Your question is unclear.

Your example "he llo" is not shifted but has a space inserted. A shift operation would result in something like "ello" (shift left by one) or " hello" (shift right by one; may be also " hell" depending on the shift definition).

Check the methods provided by your String class. There might be functions that can be used (candidates are insert(), substr(), left(), right(), mid()).

Note also that a string class is usually nothing else than a dynamic character array.


它将取决于你的应用程序运行的环境:一个CLR应用程序可以使用一个具有Substring的String类函数,而本机应用程序可能必须使用支持substr的不同字符串类。



但基本上操作将是:

It's going to depend on what environment your application is to run in: a CLR app could use one String class which has Substring functions, while a native app might have to use a different string class which supports substr instead.

But basically the operation will be along the lines of:
newstring = substring(original, 0, 1) + " " + substring(original, 1, lengthOfTheOriginalString - 1)



检查你的课程,了解要使用的确切功能和参数。



你确实意识到,实质上,任何字符串 都是 一个字符数组?


Check your classes for the exact functions and parameters to use.

And you do realise that in essence, any string is a character array?


这篇关于转换字符串中的索引?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆