Replace part of string

Hi all,

I am having an bit of trouble with replacing an part of an string in c++.

lets say i have an string that is beeing called like this
CString strIcon = pProp->szIcon;

szIcon is located in an .txt file and is called Itm_mPartSet.dds

Now what i want is that Itm_m is beeing replace by Itm_f the rest must be the same.

Also this must be done in an existing code.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
  void CItemBase::SetTexture()
{
#ifdef __CLIENT
	ItemProp* pProp = GetProp();
	if (!pProp)
	{
		LPCTSTR szErr = Error("CItemBase::SetTexture GetProp() NULL Return %d", m_dwItemId);
		ADDERRORMSG(szErr);
	}

#if __VER >= 9	// __PET_0410
	CString strIcon = pProp->szIcon;
	if (pProp->dwItemKind3 == IK3_EGG)
	{
		BYTE nLevel = 0;
		if (((CItemElem*)this)->m_pPet)
			nLevel = ((CItemElem*)this)->m_pPet->GetLevel();
		switch (nLevel)
		{
		case PL_D:
		case PL_C:
			strIcon.Replace(".", "_00.");
			break;
		case PL_B:
		case PL_A:
			strIcon.Replace(".", "_01.");
			break;
		case PL_S:
			strIcon.Replace(".", "_02.");
			break;
		}
	}
	CMover* pMover;

	if(pMover->GetSex() == MI_FEMALE)
	{
		switch (pProp->dwItemKind3)
		{
		case IK3_HELMET:
		case IK3_SUIT:
		case IK3_GAUNTLET:
		case IK3_BOOTS:
			
			break;
		}
	}
	m_pTexture = CWndBase::m_textureMng.AddTexture( g_Neuz.m_pd3dDevice, MakePath( DIR_ITEM, strIcon ), 0xffff00ff );
#else	// __PET_0410
	m_pTexture = CWndBase::m_textureMng.AddTexture( g_Neuz.m_pd3dDevice, MakePath( DIR_ITEM, GetProp()->szIcon), 0xffff00ff );
#endif	// __PET_0410
#endif
}


the code has to be located inside the Switch.

Any help and or tips will be appriciated.

With kind regards.
Last edited on
what is CString (probably some MFC stuff) and ItemProp and how do these things work is a mystery here.

why don't you just use std::string and then use std::string::replace to do replacement, and problem solved?
Last edited on
CString is MFC indeed.

Located afxstr.h

ItemProp is an structor that holds more values.

i have taken an look into std:: but didnt find an example on how to replace an part of the string.

You dont need to understand the overall code.

The basic here is

File a contains "Itm_mPartName.dds" this is stored in PropItem->szIcon

Now in here we get the szIcon make it into string so it can be replaced by something else.

Its part of an large source code of an Game. However that game has been developed many years ago and its an bit outdated. So thats why i am upgrading and improve it. But this cost time. So first things first.
By looking at MSDN documentation here is what they say about CString::Replace

https://msdn.microsoft.com/en-us/library/aa300582(v=vs.60).aspx

Now if I understood correctly, it's should be obvious why replacement in your example does not work.
You are replacing a char with string, but CString::Replace does not provide that functionality.


ignore above, maybe checking return value should help, it must not be 0.
Last edited on
Topic archived. No new replies allowed.